package cn.zl.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 获取HttpServletRequest对象
 *
 * @author zl
 * @date 2023/9/12
 */
public class HttpContextUtil {

    /**
     * 通过 Spring 的 RequestContextHolder 来获取当前线程中的
     * ServletRequestAttributes 对象，然后从该对象中获取 HttpServletRequest 对象
     */
    public static HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
}
